//Gfx functions
static const char pyGfx_BeginScene_doc[]=
"Gfx_BeginScene(target=None) -> None\n"
"Starts rendering graphics.\n"
"Note: If you use a target,please to keep at least a reference of the target.";
static PyObject *
pyGfx_BeginScene(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	PyObject * target = NULL;
	HTARGET hTarget = NULL;
	if (!PyArg_ParseTuple(args,"|O:Gfx_BeginScene",&target))
		return 0;
	if (target == NULL || target == Py_None)
		hTarget = NULL;
	else if (PyCObject_Check(target) && PyCObject_GetDesc(target)==DESC_HTARGET)
		hTarget = (HTARGET)PyCObject_AsVoidPtr(target);
	else
	{
		PyErr_SetString(PyExc_TypeError,"Target object or None is required.");
		return 0;
	}
	if (!hge->Gfx_BeginScene(hTarget))
	{
		PyErr_SetString(pyHGEError,hge->System_GetErrorMessage());
		return 0;
	}
	Py_RETURN_NONE;
}
static const char pyGfx_EndScene_doc[]=
"Gfx_EndScene() -> None\n"
"Ends rendering graphics and updates the screen if needed.";
static PyObject *
pyGfx_EndScene(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	hge->Gfx_EndScene();
	Py_RETURN_NONE;
}
static const char pyGfx_Clear_doc[]=
"Gfx_Clear(color) -> None\n"
"Clears render target (screen or texture) and z-buffer.";
static PyObject *
pyGfx_Clear(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	DWORD color;
	if (!PyArg_ParseTuple(args,"I:Gfx_Clear",&color))
		return 0;
	hge->Gfx_Clear(color);
	Py_RETURN_NONE;
}
static const char pyGfx_RenderLine_doc[]=
"Gfx_RenderLine(x1,y1,x2,y2,color=0xFFFFFFFF,z=0.5) -> None\n"
"Renders a line.";
static PyObject *
pyGfx_RenderLine(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	float x1,y1,x2,y2;
	DWORD color=0xFFFFFFFF;
	float z = 0.5;
	if (!PyArg_ParseTuple(args,"ffff|If:Gfx_RenderLine",&x1,&y1,&x2,&y2,&color,&z))
		return 0;
	hge->Gfx_RenderLine(x1,y1,x2,y2,color,z);
	Py_RETURN_NONE;
}

static const char pyGfx_Render_doc[]=
"Gfx_Render(verteis,texture = None,blend = BLEND_DEFAULT) -> None\n"
"Renders a triple or a quad,depends on the length of verteis.\n"
"verteis is a sequence of Vertex\n"
"The length of verteis must be 3 or 4.";
static PyObject *
pyGfx_Render(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	PyObject * verteis;
	PyObject * texobj = NULL;HTEXTURE tex;
	int blend = BLEND_DEFAULT;
	if (!PyArg_ParseTuple(args,"O|Oi:Gfx_Render",&verteis,&texobj,&blend))
		return 0;
	hgeTriple tri;
	hgeQuad quad;
	hgeVertex * pVert = NULL;
	//check verteis
	int length = PyObject_Length(verteis);
	if (length == -1 || (length!=3 && length!=4))
	{
		PyErr_SetString(PyExc_ValueError,"Gfx_Render(): len(verteis) must be 3 or 4.");
		return 0;
	}
	pVert = (length==3)?(tri.v):(quad.v);
	for(int i=0;i<length;++i)
	{
		PyObject * key = PyInt_FromLong(i);
		if (!key) return 0;
		PyObject * itemobj = PyObject_GetItem(verteis,key);
		Py_DECREF(key);
		if (!itemobj) return 0;
		if (!PyObject_TypeCheck(itemobj,&VertexType))
		{
			Py_DECREF(itemobj);
			PyErr_SetString(PyExc_TypeError,"Gfx_Render(): verteis must be a sequence of Vertex.");
			return 0;
		}
		pyVertex * pvert = (pyVertex*)itemobj;
		pVert[i] = pvert->v;
	}
	//texture
	if (texobj == NULL || texobj == Py_None)
		tex = NULL;
	else if (PyCObject_Check(texobj) && PyCObject_GetDesc(texobj) == DESC_HTEXTURE)
		tex = (HTEXTURE)PyCObject_AsVoidPtr(texobj);
	else
	{
		PyErr_SetString(PyExc_TypeError,"Gfx_Render(): texture must be a Texture object.");
		return 0;
	}
	if (length == 3)
	{
		tri.tex = tex;
		tri.blend = blend;
		hge->Gfx_RenderTriple(&tri);
	}
	else
	{
		quad.tex = tex;
		quad.blend = blend;
		hge->Gfx_RenderQuad(&quad);
	}
	Py_RETURN_NONE;
}
static const char pyGfx_SetClipping_doc[]=
"Gfx_SetClipping(x=0,y=0,w=0,h=0) -> None\n"
"Sets clipping region for all graphics being rendered further.";
static PyObject *
pyGfx_SetClipping(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	int x=0,y=0,w=0,h=0;
	if (!PyArg_ParseTuple(args,"|iiii:Gfx_SetClipping",&x,&y,&w,&h))
		return 0;
	hge->Gfx_SetClipping(x,y,w,h);
	Py_RETURN_NONE;
}

static const char pyGfx_SetTransform_doc[]=
"Gfx_SetTransform(x=0,y=0,dx=0,dy=0,rot=0,hscale=0,vscale=0) -> None\n"
"Sets global transformation for all graphics being rendered further.";
static PyObject *
pyGfx_SetTransform(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	float x=0.0,y=0.0,dx=0.0,dy=0.0,rot=0.0,hscale=0.0,vscale=0.0;
	if (!PyArg_ParseTuple(args,"|fffffff:Gfx_SetTransform",&x,&y,&dx,&dy,&rot,&hscale,&vscale))
		return 0;
	hge->Gfx_SetTransform(x,y,dx,dy,rot,hscale,vscale);
	Py_RETURN_NONE;
}